package com.ita.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.dao.BookDao;
import com.ita.dao.impl.BookDaoImpl;
import com.ita.pojo.Cart;

public class CheckCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public CheckCartServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		Map<Integer, Integer> map = (Map<Integer, Integer>) session
				.getAttribute("map");
		if (map == null) {
			request.getRequestDispatcher("shopcart.jsp").forward(request, response);
			return;
		} else {
			BookDao bookDao = new BookDaoImpl();
			List<Cart> carts = new ArrayList<Cart>();
			for (Integer bookid : map.keySet()) {
				Integer bookNum = map.get(bookid);
				String bookname = bookDao.getBook(bookid).getBookname();
				Cart cart = new Cart(bookid,bookname, bookNum + "");
				carts.add(cart);
			}
			session.setAttribute("carts", carts);
			request.getRequestDispatcher("shopcart.jsp").forward(request, response);
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
